package com.dyzx.hbb.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseCustomController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.common.utils.Utils;
import com.dyzx.hbb.modules.sys.entity.SysOrder;
import com.dyzx.hbb.modules.sys.entity.SysUser;
import com.dyzx.hbb.modules.sys.query.OrderQuery;
import com.dyzx.hbb.modules.sys.service.SysOrderService;
import com.dyzx.hbb.modules.sys.vo.SysOrderUVO;
import com.dyzx.hbb.modules.sys.vo.SysOrderVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/sys/order")
@RestController
@Tag(name = "13.订单管理", description = "订单相关接口")
public class OrderController extends BaseCustomController<SysOrderService, SysOrder, OrderQuery> {

    @Autowired
    private SysOrderService service;


    @Override
    protected SysOrderService getBaseService() {
        return service;
    }


    @Override
    protected MyLambdaQueryWrapper<SysOrder> queryWrapper(OrderQuery query) {
        MyLambdaQueryWrapper<SysOrder> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysOrder::getId, SysOrder::getOrderNo, SysOrder::getType, SysOrder::getPayMode,SysOrder::getPayDate, SysOrder::getPayOrderNo, SysOrder::getStatus, SysOrder::getCreateDate, SysOrder::getUpdateDate,SysOrder::getAmt).userName(SysOrder::getOperatorId).as("operator_name");

      //  wrapper.imageUrl(SysOrder::getImage).as("image_url");
        if (query != null) {

            wrapper.like(StringUtils.isNotBlank(query.getOrderNo()), SysOrder::getOrderNo, query.getOrderNo())
                    .like(StringUtils.isNotBlank(query.getPayOrderNo()), SysOrder::getPayOrderNo, query.getPayOrderNo())
                    .inSet(StringUtils.isNotBlank(query.getType()), SysOrder::getType, query.getType())
                    .inSet(StringUtils.isNotBlank(query.getPayMode()), SysOrder::getPayMode, query.getPayMode())
                    .inSet(query.getStatus() != null, SysOrder::getStatus, query.getStatus())
                    .dateBetween(StringUtils.isNotBlank(query.getCreateDate()), SysOrder::getCreateDate, DateFormat.DATE, query.getCreateDate())
             .dateBetween(StringUtils.isNotBlank(query.getPayDate()), SysOrder::getPayDate, DateFormat.DATE, query.getPayDate());
            ;

        }
        return wrapper;

    }


    @Override
    protected MyLambdaQueryWrapper<SysOrder> queryInfoWrapper() {
        MyLambdaQueryWrapper<SysOrder> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysOrder::getId, SysOrder::getOrderNo, SysOrder::getType, SysOrder::getPayMode,SysOrder::getPayDate, SysOrder::getPayOrderNo, SysOrder::getStatus, SysOrder::getCreateDate, SysOrder::getUpdateDate).userName(SysOrder::getOperatorId).as("operator_name");

        wrapper.select(SysOrder::getStatus, SysOrder::getCreateDate, SysOrder::getUpdateDate,SysOrder::getAmt).userName(SysOrder::getOperatorId).as("operator_name");

        wrapper.imageUrl(SysOrder::getImage).as("image_url");

        return wrapper;
    }


    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        if (id == null) {
            return Result.error("id不能为空");
        }
        Long organId = SecurityContextHolder.getOrganId();
        QueryWrapper<SysOrder> queryWrapper = new QueryWrapper<>();
        SysOrder byId = getBaseService().getOne(queryWrapper.eq("id", id).eq("organ_id", organId).last("limit 1"));
        if (byId == null) {
            return Result.error("数据不存在");
        }

        boolean data = getBaseService().removeById(id);
        return data ? Result.ok() : Result.error("删除失败");
    }

    //添加订单
    @Operation(summary = "添加订单")
    @PostMapping
    public Result<Boolean> add(SysOrderVO sysOrder) {
        if (sysOrder == null) {
            return Result.error("参数不能为空");
        }
        Long organId = SecurityContextHolder.getOrganId();
        Long userId = SecurityContextHolder.getUserId();
        SysOrder entity = new SysOrder();
        entity.setOrderNo(Utils.getOrderNum(String.valueOf(organId)));
        BeanUtil.copyProperties(sysOrder, entity);
        entity.setOperatorId(userId);
        return getBaseService().save(entity) ? Result.ok() : Result.error("保存失败");
    }

    @Operation(summary = "支付订单")
    @PutMapping("pay")
    public Result<Boolean> add(SysOrderUVO sysOrder) {
        if (sysOrder == null) {
            return Result.error("参数不能为空");
        }
        SysOrder entity =getBaseService().getById(sysOrder.getId());
        if (entity == null) {
            return Result.error("订单不存在");
        }

        BeanUtil.copyProperties(sysOrder, entity);
        entity.setStatus(1);
    //    entity.setPayDate(System.currentTimeMillis());

        return getBaseService().updateById(entity) ? Result.ok() : Result.error("保存失败");
    }


}
